library(ggplot2)
library(tidyr)
library(tidyverse)
library(estimatr)
library(gridExtra)
library(coefplot)
library(dplyr)
library(patchwork)

# Figure 4: Supporting Nuclear Use At Any Threshold + Average Treatment Effect

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_3/")

Dat <- read.csv("Paper_3_Data_Clean.csv", stringsAsFactors = FALSE)


fit <- lm_robust(Nuclear ~ Treatment, data = filter(Dat, NuclearTheory == 1))

summary(fit)

gg_df_1 <-
  Dat %>% 
  mutate(ever = Nuclear) %>% 
  group_by(Treatment, NuclearTheory) %>% 
  summarize(tidy(lm_robust(ever ~ 1, data = cur_data())))

gg_df_1 <-
  gg_df_1 %>% 
  mutate(term_label =
         factor(
           Treatment,
           levels = c("Control",
                      "FriendPro-NuclearMoral",
                      "FriendPro-NuclearUtilitarian",
                      "SenatorPro-NuclearMoral",
                      "SenatorPro-NuclearUtilitarian"),
           labels = c(
             "Control",
             "Peer\nMoral",
             "Peer\nUtilitarian",
             "Elite\nMoral",
             "Elite\nUtilitarian"
           )
         ),
       
       theory_label = factor(NuclearTheory, levels = c(1, 2, 3, 4),
                                  labels = c("Never-Ever", "Deterrence", "Offense/Defense","Utilitarian"))
)



label_df <- 
  gg_df_1 %>% 
  filter(term_label == "Elite\nUtilitarian")

pos <- position_dodgev(height = 0.5)

g1 <- 
ggplot(gg_df_1, aes(x = estimate, y = term_label, color = theory_label, group = theory_label,shape= theory_label)) +
  geom_point(position = pos) +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high), position = pos) +
  geom_text(data = label_df, aes(label = theory_label, x = conf.low - 0.06), position = position_dodgev(height = 0.5)) +
  theme_bw() + 
  scale_color_manual(values=c("green3","yellow3","orange3", "red3")) + 
  theme(legend.position="none", plot.title = element_text(hjust = 0.5)) +
  ylab("Treatment")+
  xlab("Proportion")+
  ggtitle("Proportion Supporting Nuclear Use At Any Threshold")
  



gg_df_2 <-
  Dat %>% 
  mutate(ever = Nuclear) %>% 
  group_by(NuclearTheory) %>% 
  summarize(tidy(lm_robust(ever ~ Treatment, data = cur_data()))) %>% 
  filter(term != "(Intercept)")

gg_df_2 <-
  gg_df_2 %>% 
  mutate(term_label =
           factor(
             term,
             levels = c(
                        "TreatmentFriendPro-NuclearMoral",
                        "TreatmentFriendPro-NuclearUtilitarian",
                        "TreatmentSenatorPro-NuclearMoral",
                        "TreatmentSenatorPro-NuclearUtilitarian"),
             labels = c(
               "Peer\nMoral",
               "Peer\nUtilitarian",
               "Elite\nMoral",
               "Elite\nUtilitarian"
             )
           ),
         
         theory_label = factor(NuclearTheory, levels = c(1, 2, 3, 4),
                               labels = c("Never-Ever", "Deterrence", "Offense/Defense","Utilitarian"))
  )


label_df_2 <- 
  gg_df_2 %>% 
  filter(term_label == "Elite\nUtilitarian")

pos <- position_dodgev(height = 0.5)

g2 <- 
ggplot(gg_df_2, aes(x = estimate, y = term_label, color = theory_label, group = theory_label, shape= theory_label)) +
  geom_point(position = pos) +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high), position = pos) +
  geom_text(data = label_df_2, aes(label = theory_label, x = conf.low - 0.06), position = position_dodgev(height = 0.5)) +
  geom_vline(xintercept=0, linetype="dotted")+
  theme_bw() + 
  scale_color_manual(values=c("green3","yellow3","orange3", "red3")) + 
  theme(legend.position="none", plot.title = element_text(hjust = 0.5)) +
  ggtitle("Average Treatment Effect Estimate")+
  ylab("Treatment")+
  xlab("Estimate")+
  coord_cartesian(ylim=c(0,4), xlim=c(-0.3,0.3))


g<-grid.arrange(g1, g2, ncol=1)


ggsave("Image_4.png", plot=g, width=2200, height=4000, units=c("px"))

